package ms.pc.inf;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;

public class Authentication extends Activity {

	private EditText user,pass;
	private Button submitButton,cancelButton;

	public void onCreate(final Bundle icicle){
		super.onCreate(icicle);
		setContentView(R.layout.authorization);
		final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
		this.user = (EditText) findViewById(R.id.username);
		this.pass = (EditText) findViewById(R.id.password);

		this.submitButton = (Button) findViewById(R.id.login);
		this.submitButton.setOnClickListener(new OnClickListener() {
			public void onClick(final View v) {
				DBConnectivity objDB = new DBConnectivity();
				String use = user.getText().toString();
				String pas = pass.getText().toString();
				//objDB.authenticate("mohsin", "mohsin");
				//if(objDB.authenticate("mohsin", "mohsin")){
				if (objDB.authenticate(use, pas)) {
				//	if(use.equals(pas))
					//{
					System.out.println("Login Succesful");
					showEvents();
				} 
				else {
					System.out.println("Login Failed");
					//Toast.makeText(this, "Wrong Usernam/Password", 5);
					alertDialog.setTitle("Invalid credential")
					.setMessage("Please check your username or password !!!!")
					.setNegativeButton("Try Again", new DialogInterface.OnClickListener() {  
						public void onClick(DialogInterface dialog, int which) {  
							return;  
						} }); 
					alertDialog.show();
					user.setText("");
					pass.setText("");
				}
			}
		});
		this.cancelButton = (Button) findViewById(R.id.cancel);
		this.cancelButton.setOnClickListener(new OnClickListener() {
			public void onClick(final View v) {
				user.setText("");
				pass.setText("");
			}
		});

	}
	private void showEvents()
	{
		//startActivity(new Intent(this, Events.class));
		startActivity(new Intent(this, Tab.class));
		this.finish();
	}

}
